%this function gets a seed coordinate and expand a 3D region around it
%according gray level similarity
%x - col, y - row, z - height
function [Size] = segmentationHelper(x,y ,z)
global segMatrix;
global image;
global DIFF_FACTOR;
DIFF_FACTOR
    origGrayLevel = image(y,x,z)
    %prepare the segmentation matrix
    TH = zeros(size(image));
    
    %mark color-related voxels with "1"
    TH(logical((origGrayLevel*(1-DIFF_FACTOR)<=image) & (origGrayLevel*(1+DIFF_FACTOR)>=image))) = 1;
    TH(y,x,z)
    %find all connected component of "1"s
    CC = bwconncomp(TH,26);
    L = labelmatrix(CC);
    
    TH = zeros(size(image));
    TH(CC.PixelIdxList{L(y,x,z)})=1;
    segMatrix = TH;
    Size=0;
    return;
    PixelIdxList = CC.PixelIdxList;
    
    %find the connected component which the clicked pixels belongs to
    origPixelIndex = (z-1)*size(image,1)*size(image,2) + (x-1)*size(image,1) + y;

    for i=1:length(PixelIdxList)    
        
        res = find(PixelIdxList{i} == origPixelIndex);
        
        if (size(res,1) == 0)
            continue;
        end
        
        %mark all the connected component's voxels
        segMatrix = zeros(size(image));
        segMatrix(PixelIdxList{i}) = 1;
        segMatrix = logical(segMatrix);
        Size = length(PixelIdxList{i});
        break;
        
    end
    
end